#include <iostream>
#include "nature.h"
#include "wolf.h"
#include "sheep.h"
#include "grass.h"

using namespace std;

void Nature::start()
{
	cout << "请输入总时间" << endl;
	cin >> time;
}

void Nature::create(int num_wolf, int num_sheep, int num_grass)
{

	for (int i = 1; i <= num_wolf; i++)
	{
		livings[num.all] = new Wolf();
		num.all++;
	}
	for (int i = 1; i <= num_sheep; i++)
	{
		livings[num.all] = new Sheep();
		num.all++;
	}
	for (int i = 1; i <= num_grass; i++)
	{
		livings[num.all] = new Grass();
		num.all++;
	}
}

void Nature::turns() /*每个生物每回合的行动，应该可以直接写个函数封装在生物类里面*/
{
	for (int i = 1; i <= num.all; i++)
	{
		/*TODO 跳过条件：死了还是空指针？怎么设置，要不要当场析构*/
		livings[i]->action();
		/*最后总结时有没有场外操作，比如死了给析构掉*/
		if (livings[i] /*死了*/)
		{
			delete livings[i];
		}
	}
}

void Nature::result()
{
	cout << "模拟结束,共经过" << time_past << "单位时间" << endl;
	stillalive();
	cout << "场地上还剩狼" << num_end.wolf << "只，羊" << num_end.sheep << "只，草" << num_end.grass << "株" << endl;
	cout << "共发生" << num.events << "起事件" << endl;
	choose_result();
	cout << "程序结束" << endl;
}

void Nature::function()
{
	char temp;
	cout << "请选择" << endl;
	cout << "1、只输入个数，完全随机生成" << endl;
	cout << "2、预设生成，一狼一羊检验捕食功能" << endl;
	cout << "3、预设生成，两狼验证繁衍功能" << endl;
	cout << "4、预设生成，一狼验证老死" << endl;
	cout << "5、预设生成，一狼验证饿死" << endl;
	cin >> temp;
	switch (temp)
	{
	case '1':
		cout << "请输入狼、羊、草的数量" << endl;
		cin >> num.wolf >> num.sheep >> num.grass;
		create(num.wolf, num.sheep, num.grass);
		break;
	case '2': /*检验捕猎*/
		num = {1, 1, 0, 0};
		create(1, 1, 0);
		wolf[1]->m_position = {0, 0};
		sheep[1]->m_position = {1, 1};
		wolf[1]->m_hunger = 20;
		break;
	case '3':
		num = {2, 0, 0, 0};
		create(2, 0, 0);
		wolf[1]->m_flag.gender = 1;
		wolf[2]->m_flag.gender = 2;
		wolf[1]->m_position = {0, 0};
		wolf[2]->m_position = {1, 1};
		wolf[1]->m_hunger = 200;
		wolf[2]->m_hunger = 200;
		break;
	case '4':
		num = {1, 0, 0, 0};
		create(1, 0, 0);
		wolf[1]->m_age = 21;
		break;
	case '5':
		num = {1, 0, 0, 0};
		create(1, 0, 0);
		wolf[1]->m_hp = 20;
		wolf[1]->m_hunger = 0;
		break;
	default:
		cout << "输入非法，请重新输入" << endl;
		function();
		break;
	}
}

void Nature::stillalive()
{
	for (int i = 1; i <= num.wolf; i++)
	{
		if (wolf[i] != nullptr)
		{
			if (wolf[i]->m_flag.live == 1)
			{
				num_end.wolf++;
			}
		}
	}
	for (int i = 1; i <= num.sheep; i++)
	{
		if (sheep[i] != nullptr)
		{
			if (sheep[i]->m_flag.live == 1)
			{
				num_end.sheep++;
			}
		}
	}
	for (int i = 1; i <= num.grass; i++)
	{
		if (grass[i] != nullptr)
		{
			if (grass[i]->m_flag.live == 1)
			{
				num_end.grass++;
			}
		}
	}
}

void Nature::choose_result()
{
	char temp_e = '0';
	int flag_spi = 0; /*物种标签*/
	int flag_evn = 0; /*事件标签*/
	cout << "请选择事件查看模式，按*直接退出" << endl;
	cout << "1、默认查看" << endl
		 << "2、全部查看" << endl
		 << "3、按物种查看" << endl
		 << "4、按事件查看" << endl;
	cin >> temp_e;
	switch (temp_e)
	{
	case '1': /*默认则只输出狼的狩猎、全部繁衍和全部死亡*/
		for (int i = 1; nature.events[i] != nullptr; i++)
		{
			if (nature.events[i]->m_flag_do == 2 || nature.events[i]->m_flag_do == 3 || nature.events[i]->m_species_o == 1)
			{
				nature.events[i]->show();
			}
		}
		break;
	case '2':
		break;
	case '3':
		cout << "请输入物种，狼为1，羊为2，草为3" << endl;
		cin >> flag_spi;
		break;
	case '4':
		cout << "请输入事件，狩猎为1，繁衍为2，死亡为3" << endl;
		cin >> flag_evn;
		break;
	case '*':
		return;
		break;
	default:
		break;
	}
	for (int i = 1; nature.events[i] != nullptr; i++)
	{
		if (flag_evn == 0 || nature.events[i]->m_flag_do == flag_evn)
		{
			if (flag_spi == 0 || nature.events[i]->m_species_o == flag_spi)
			{
				nature.events[i]->show();
			}
		}
	}
}